import { judgePort } from '@/units'

const SessionMap = ['ModuleAnchorOpera']

const useSessionStorage = () => {
  const setSession = (key, value) => {
    return sessionStorage.setItem(`${key}_${judgePort()}`, value)
  }

  const getSession = (key) => {
    return sessionStorage.getItem(`${key}_${judgePort()}`)
  }

  const clearSession = () => {
    SessionMap.map((key) => {
      removeSession(key)
    })
  }

  const removeSession = (key) => {
    sessionStorage.removeItem(`${key}_${judgePort()}`)
  }
  return {
    setSession,
    getSession,
    clearSession,
    removeSession,
  }
}

const LocalMap = [
  'loginMode',
  'USERSTORE',
  'tokenExpires',
  'WEATHER',
  'AllModulesList',
  'token',
  'beforePathObjStorage',
]

const useLocalStorage = () => {
  const setLocal = (key, value) => {
    return localStorage.setItem(`${key}_${judgePort()}`, value)
  }

  const getLocal = (key) => {
    return localStorage.getItem(`${key}_${judgePort()}`)
  }

  const clearLocal = () => {
    LocalMap.map((key) => {
      removeLocal(key)
    })
  }

  const removeLocal = (key) => {
    localStorage.removeItem(`${key}_${judgePort()}`)
  }

  return {
    setLocal,
    getLocal,
    clearLocal,
    removeLocal,
  }
}

export { useLocalStorage, useSessionStorage }
